const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const userService = require('../service/user')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

var allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
  res.header('Access-Control-Allow-Headers', 'Content-Type')
  res.header('Access-Control-Allow-Credentials','true')
  next()
}
app.use(allowCrossDomain)

app.post('/api/user/login', (req, res) => {
  userService.login(req.body).then(data => {
    res.send(data)
  }).catch(err => {
    res.send(err)
  })
})

app.get('/api', (req, res) => {
  res.send('nihao')
})

module.exports = app
